<?php
namespace app\admin\controller;


use think\Db;
class AppletAnnounce extends Common{
    public function announceList(){
        $data=request()->get();
        $whe["del_status"]=2;
        if(!empty($data)){

        }
        $list=Db::name('applet_announce')->where($whe)->order('id desc')->paginate(20);
        foreach($list as $value){

        }
        /*if(isset($votelist)){
            $this->assign('votelist',$votelist);
        }else{
            $this->assign('votelist',$list);
        }*/
        $this->assign("announcelist",$list);
        $this->assign("list",$list);
        $number=Db::name('applet_announce')->where($whe)->count();
        $this->assign("number",$number);
        foreach($data as $k => $v) {
            $this->assign($k, $v);
        }
        return $this->fetch("announce_list");
    }

    public function announceAdd(){
        if(request()->isPost()){
            $data=request()->post();
            //var_dump($data);die;
            $file = request()->file('bannerurl');

            if(!empty($file)){

                $info = $file->rule('sha1')->move(ROOT_PATH . 'public' . DS . 'uploads'. DS .'icon');
                if($info){
                    $name=str_replace('\\', '/',$info->getSaveName());
                    $data['bannerurl'] = "https://www.zz.zfbzhsq.com/uploads/icon/".$name;
                    //var_dump($data);die;
                }else{
                    echo $file->getError();
                    die;
                }
            }else{
                $this->error("请选择要上传的图片");
            }
            $res=Db::table("applet_announce")->insert($data);
            if($res){
                return "添加成功";
            }else{
                return "添加失败";
            }
        }else{
            return $this->fetch("announce_add");
        }
    }

    public function announceEdit(){
        if(request()->isPost()){
            $data=request()->post();
            $file = request()->file('bannerurl');
            //var_dump($data);
            //var_dump(expression)
            if(!empty($file)){

                $info = $file->rule('sha1')->move(ROOT_PATH . 'public' . DS . 'uploads'. DS .'icon');
                if($info){
                    $name=str_replace('\\', '/',$info->getSaveName());
                    $data['bannerurl'] = "https://www.zz.zfbzhsq.com/uploads/icon/".$name;
                    //var_dump($data);die;
                }else{
                    echo $file->getError();
                    die;
                }
            }
            $res=Db::table("applet_announce")->update($data);
            if($res){
                return "修改成功";
            }else{
                return "修改失败";
            }
        }else{
            $id=request()->get("id");
            $info=Db::table("applet_announce")->where("id",$id)->find();
            $this->assign("info",$info);
            return $this->fetch("announce_edit");
        }
    }


    public function announceCon(){
        if(request()->isPost()){
            $data=request()->post();
            $file = request()->file('bannerurl');
            //var_dump($data);
            //var_dump(expression)
            if(!empty($file)){

                $info = $file->rule('sha1')->move(ROOT_PATH . 'public' . DS . 'uploads'. DS .'icon');
                if($info){
                    $name=str_replace('\\', '/',$info->getSaveName());
                    $data['bannerurl'] = "https://www.zz.zfbzhsq.com/uploads/icon/".$name;
                    //var_dump($data);die;
                }else{
                    echo $file->getError();
                    die;
                }
            }
            $res=Db::table("applet_announce")->update($data);
            if($res){
                return "修改成功";
            }else{
                return "修改失败";
            }
        }else{
            $id=request()->get("id");
            $info=Db::table("applet_announce")->where("id",$id)->find();
            $this->assign("info",$info);
            return $this->fetch("announce_con");
        }
    }

    public function announceDel(){
        $id=request()->get("id");
        $info=Db::table("applet_announce")->where("del_status",2)->where("id",$id)->find();
        if(empty($info)){
            $this->error("改数据不存在");
        }
        $res=Db::table("applet_announce")->where("id",$id)->update(["del_status"=>"1","del_name"=>session('name')]);
        if($res){
            //return "删除成功";
            $this->success("删除成功");
        }else{
            $this->error("删除失败");
        }

    }



}